#Get acceleration data from Chronos watch.
#Taken from info posted at: http://e2e.ti.com/support/microcontrollers/msp43016-bit_ultra-low_power_mcus/f/166/t/32714.aspx
#
#FIXED: In old version I had the x and z value switched around.
#
#
# Copyright (c) 2010 Sean Brewer 
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
#
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
#
#
#If you want you may contact me at seabre986@gmail.com
#or on reddit: seabre
#


import serial
import array
import struct
import math
from Tkinter import *
import chronos


def startAccessPoint():
    return array.array('B', [0xFF, 0x07, 0x03]).tostring()

def accDataRequest():
    return array.array('B', [0xFF, 0x08, 0x07, 0x00, 0x00, 0x00, 0x00]).tostring()


root = Tk()
canvas = Canvas(root, width=300, height=300)  
canvas.pack(expand=YES, fill=BOTH)                

canvas.create_oval(150, 150, 160,160, width=1, fill='blue', tag='blueBall')
canvas.create_text(60,10,text='0.0',tags='x_text')
canvas.create_text(60,20,text='0.0',tags='y_text')
canvas.create_text(60,30,text='0.0',tags='z_text')


chronos.initAcc()


while True:
    angles = chronos.readAngles()
    if len(angles) == 3:
    	rho = angles[0]
    	phi = angles[1]
    	theta = angles[2]
    	print "rho: " + str(rho)+ " phi: " + str(phi) + " theta: " + str(theta)
    	deltay = (int)((rho/90)*3)
    	deltax = (int)((phi/90)*3)
    	canvas.move('blueBall', deltax, deltay)
    	canvas.itemconfigure('x_text',text="rho: " + str(rho))
    	canvas.itemconfigure('y_text',text="phi: " + str(phi))
    	canvas.itemconfigure('z_text',text="theta: " + str(theta))
    canvas.update()
root.mainloop()    	   
ser.close()
